﻿Imports System.Exception
Imports Microsoft.Office.Interop.Outlook

Namespace UI
    Public Class pmNewFolderForm
        Inherits PMOutlookAddIn.UI.pmBaseFolderTreeForm

        Protected Friend _newFolder As MAPIFolder
        Private _folderToSelectOnLoad As MAPIFolder
        Private _suggestedFoldername As String
        Private _Email As String

        Dim _pmNewContact As pmNewContact

        Public Sub New(ByVal App As Application, ByVal folderToSelect As MAPIFolder, ByVal suggestedFoldername As String, ByVal Email As String)
            MyBase.New(App)
            InitializeComponent()
            _folderToSelectOnLoad = folderToSelect
            _suggestedFoldername = suggestedFoldername
            _Email = Email
        End Sub

#Region "Private methods"
        Private Sub pmNewFolderForm_ActionButtonClicked() Handles Me.ActionButtonClicked
            Try
                Me.DialogResult = Nothing
                Dim baseFolder As Outlook.MAPIFolder = _pmFoldertree.GetSelectedFolder()

                If Not IsNothing(baseFolder) Then
                    If txtFoldernameBox.Text = String.Empty Then
                        MessageBox.Show("Gelieve een naam op te geven voor de nieuwe map")
                    Else
                        If _pmFoldertree.GetFolderByName(baseFolder, txtFoldernameBox.Text) Is Nothing Then
                            _newFolder = baseFolder.Folders.Add(txtFoldernameBox.Text, OlDefaultFolders.olFolderInbox)

                            If rbKlant.Checked Then
                                'toevoegen contact
                                Dim newContact As Outlook.ContactItem = CType(_app.CreateItem(OlItemType.olContactItem), ContactItem)
                                With newContact
                                    .FirstName = _pmNewContact.txtVoornaam.Text
                                    .LastName = _pmNewContact.txtAchternaam.Text
                                    .CompanyName = _pmNewContact.txtBedrijf.Text
                                    .Email1Address = _pmNewContact.txtEmail.Text
                                End With
                                newContact.Save()
                            End If

                            DialogResult = Windows.Forms.DialogResult.OK
                            Me.Close()
                        Else
                            MessageBox.Show("Er bestaat al een map met deze naam. Gebruik een andere naam.")
                            SharedMethods.ControlSetFocus(txtFoldernameBox)
                            txtFoldernameBox.SelectAll()
                        End If
                    End If
                Else
                    MessageBox.Show("Gelieve een map te selecteren.")
                End If
            Catch ex As System.Exception
                ExceptionHandler.ShowAndLog("Er is een fout opgetreden bij het toevoegen van een map.", ex)
            End Try
        End Sub
#End Region

        Public Overrides Sub pmForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs)
            SharedMethods.ControlSetFocus(txtFoldernameBox)
            SelectFolder(_folderToSelectOnLoad)
        End Sub

        Private Sub pmNewFolderForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            txtFoldernameBox.Text = _suggestedFoldername
            txtFoldernameBox.SelectionStart = _suggestedFoldername.Length
        End Sub

        Private Sub rbKlant_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbKlant.CheckedChanged
            If rbKlant.Checked Then
                _pmNewContact = New pmNewContact(Me._app, _Email)
                If _pmNewContact.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then

                    txtFoldernameBox.Text = _pmNewContact.txtVoornaam.Text + Space(1) + _pmNewContact.txtAchternaam.Text + " - " + _pmNewContact.txtBedrijf.Text
                    txtFoldernameBox.ReadOnly = True

                    'initializeTreeview()
                    'SelectFolder(_pmNewFolderForm._newFolder)
                    '_pmNewFolderForm.Dispose()

                Else
                    rbOrder.Checked = True
                    SharedMethods.ControlSetFocus(txtFoldernameBox)
                End If
            Else
                txtFoldernameBox.Text = ""
                txtFoldernameBox.ReadOnly = False
            End If
        End Sub

        Private Sub rbOrder_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbOrder.CheckedChanged
            If rbOrder.Checked Then
                txtFoldernameBox.Text = ""
                txtFoldernameBox.ReadOnly = False
                SharedMethods.ControlSetFocus(txtFoldernameBox)
            End If
        End Sub
    End Class
End Namespace